<?php
/**
 * Author : CuongNV
 */
class CateroomsController extends AppController {

	public function index() {
		$this->Cateroom->recursive = 0;
		$this->set('caterooms', $this->Paginator->paginate());
	}

	public function view($id = null) {
		if (!$this->Cateroom->exists($id)) {
			throw new NotFoundException(__('Invalid cateroom'));
		}
		$options = array('conditions' => array('Cateroom.' . $this->Cateroom->primaryKey => $id));
		$this->set('cateroom', $this->Cateroom->find('first', $options));
	}

	public function get_people(){
		$this->autoRender = false;
		$data = json_decode($_POST["data"]);
		$this->Cateroom->recursive = -1;
		$people = $this->Cateroom->find('first',array(
			'conditions' => array('Cateroom.id' => $data->id),
			'fields' => 'max_people'
		));
		$p = $people['Cateroom']['max_people'];
		echo json_encode($p);
	}

	public function get_first(){
		$this->autoRender = false;
		$data = json_decode($_GET["id"]);
		$result = $this->Cateroom->find('first',array(
			'conditions' => array('Cateroom.id' => $data->key),
			'fields' => array('Cateroom.id', 'Cateroom.name', 'Cateroom.note', 'Cateroom.plenty')
		));
		echo json_encode($result);
	}

	public function access_price($id = null){
		$this->loadModel('PriceTime');
		$this->loadModel('OverPeople');
		if($this->request->is('post')){
			$temp = $this->request->data;
			$this->Cateroom->read(null, $temp['Cateroom']['id']);
			$this->Cateroom->set(array(
			    'price_date' => $temp['Cateroom']['price_date'],
			    'price_week' => $temp['Cateroom']['price_week'],
			    'price_month' => $temp['Cateroom']['price_month']
			));
			$this->Cateroom->save();
			if(isset($temp['PriceTime'])){
				foreach ($temp['PriceTime'] as $val1) {
					$this->PriceTime->create();
					if ($val1['time'] == '' || $val1['price'] == '') {
						continue;
					}
					$this->PriceTime->set(array(
					    'time' => $val1['time'],
					    'price' => $val1['price'],
					    'type' => 1,
					    'caterooms_id' => $temp['Cateroom']['id']
					));
					$this->PriceTime->save();
				}
			}
			if(isset($temp['PriceTimed'])){
				foreach ($temp['PriceTimed'] as $val2) {
					$this->PriceTime->create();
					if ($val2['time'] == '' || $val2['price'] == '') {
						continue;
					}
					$this->PriceTime->set(array(
					    'time' => $val2['time'],
					    'price' => $val2['price'],
					    'type' => 2,
					    'caterooms_id' => $temp['Cateroom']['id']
					));
					$this->PriceTime->save();
				}
			}
			if(isset($temp['OverPeople'])){
				foreach ($temp['OverPeople'] as $val3) {
					$this->OverPeople->create();
					$this->OverPeople->set(array(
					    'people' => $val3['people'],
					    'price' => $val3['price']
					));
					$this->OverPeople->save();
				}
			}

			foreach ($temp['Cateroom']['idop'] as $k3 => $v3) {
				$this->OverPeople->read(null, $v3);
				$this->OverPeople->set(array(
				    'people' => $temp['Cateroom']['people'][$k3],
				    'price' => $temp['Cateroom']['priceop'][$k3]
				));
				$this->OverPeople->save();
			}

			foreach ($temp['Cateroom']['idpth'] as $k1 => $v1) {
				$this->PriceTime->read(null, $v1);
				$this->PriceTime->set(array(
				    'time' => $temp['Cateroom']['timeh'][$k1],
				    'price' => $temp['Cateroom']['priceh'][$k1]
				));
				$this->PriceTime->save();
			}
			foreach ($temp['Cateroom']['idptn'] as $k2 => $v2) {
				$this->PriceTime->read(null, $v2);
				$this->PriceTime->set(array(
				    'time' => $temp['Cateroom']['timeon'][$k2],
				    'price' => $temp['Cateroom']['priceon'][$k2]
				));
				$this->PriceTime->save();
			}
			$this->redirect(array('controller' => 'rooms', 'action' => 'setup_price'));
		}
		$over_people = $this->OverPeople->find('all');
		$pr = $this->Cateroom->find('first', array('conditions' => array('Cateroom.id' => $id)));
		$prt = $this->PriceTime->find('all', array('conditions' => array('PriceTime.caterooms_id' => $id)));
		$this->set(compact('pr', 'prt', 'id', 'over_people'));
	}

	public function list_price(){;
		$this->autoRender = false;
		$data = json_decode($_GET["data"]);
		$this->Cateroom->recursive = 1;
		$result = $this->Cateroom->find('first', array(
			'conditions' => array('Cateroom.id' => $data->id),
			'contain' => array('PriceTime')
		));
		echo json_encode($result);
	}

	public function add() {
		if ($this->request->is('post')) {
			$this->Cateroom->create();
			$this->request->data['Cateroom']['order'] = 1;
			$this->request->data['Cateroom']['price_date'] = str_replace(',', '', $this->request->data['Cateroom']['price_date']);
			$this->request->data['Cateroom']['price_week'] = str_replace(',', '', $this->request->data['Cateroom']['price_week']);
			$this->request->data['Cateroom']['price_month'] = str_replace(',', '', $this->request->data['Cateroom']['price_month']);
			$this->request->data['Cateroom']['name'] = strip_tags($this->request->data['Cateroom']['name']);
			$this->request->data['Cateroom']['plenty'] = strip_tags($this->request->data['Cateroom']['plenty']);
			$this->request->data['Cateroom']['note'] = strip_tags($this->request->data['Cateroom']['note']);
			$this->Cateroom->save($this->request->data);
			return $this->redirect(array('controller'=>'rooms','action' => 'index'));
		}
	}
	public function edit($id = null) {
		if (!$this->Cateroom->exists($id)) {
			return $this->redirect(array('controller' => 'rooms', 'action' => 'index'));
		}
		if ($this->request->is(array('post', 'put'))) {
			$this->request->data['Cateroom']['id'] = $id;
			$this->request->data['Cateroom']['price_date'] = str_replace(',', '', $this->request->data['Cateroom']['price_date']);
			$this->request->data['Cateroom']['price_week'] = str_replace(',', '', $this->request->data['Cateroom']['price_week']);
			$this->request->data['Cateroom']['price_month'] = str_replace(',', '', $this->request->data['Cateroom']['price_month']);
			$this->request->data['Cateroom']['name'] = strip_tags($this->request->data['Cateroom']['name']);
			$this->request->data['Cateroom']['plenty'] = strip_tags($this->request->data['Cateroom']['plenty']);
			$this->request->data['Cateroom']['note'] = strip_tags($this->request->data['Cateroom']['note']);
			$this->Cateroom->save($this->request->data);
			return $this->redirect(array('controller' => 'rooms', 'action' => 'index'));
		}else {
			$options = array('conditions' => array('Cateroom.' . $this->Cateroom->primaryKey => $id));
			$this->request->data = $this->Cateroom->find('first', $options);
		}
	}

	public function delete($id = null) {
		$this->Cateroom->id = $id;
		if (!$this->Cateroom->exists()) {
			return $this->redirect(array('controller'=>'rooms','action' => 'index'));
		}
		$this->loadModel('Room');
		$check = $this->Room->find('first', array('conditions' => array('Room.caterooms_id' => $id)));
		if($check){
			$link = "http://".$_SERVER["HTTP_HOST"].$this->webroot."rooms";
			echo '<script>';
			echo 'alert("Loại phòng đang tồn tại phòng không thể xóa !");';
			echo "window.location.href = '<?php echo $link; ?>' ";
			echo '</script>';
		}else{
			$this->Cateroom->delete();
			return $this->redirect(array('controller'=>'rooms','action' => 'index'));
		}
	}


}
